<template>
  <div class="dashboard-container">
    <h1 style="text-align: center">订单管理</h1>

    <el-dialog title="确认收款" :visible.sync="collectionController" width="30%" @close="closeCollection('ruleForm')">
      <el-form label-width="20%" ref="ruleForm">
        <p style="text-align: center; font-size: 16px;">确认是否收款</p>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="info" plain @click="collectionAllLive('ruleForm')">确 定</el-button>
        <el-button plain @click="closeCollection('ruleForm')">取 消</el-button>
      </span>
    </el-dialog>
    <el-dialog title="结算" :visible.sync="settlementLiveController" width="30%" @close="closeSettlement('ruleForm')">
      <el-form label-width="20%" ref="ruleForm">
        <p style="text-align: center; font-size: 16px;">确认是否结算</p>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="info" plain @click="settlementAllLive('ruleForm')">确 定</el-button>
        <el-button plain @click="closeSettlement('ruleForm')">取 消</el-button>
      </span>
    </el-dialog>

    <el-select v-model="state" clearable placeholder="订单状态" style="margin-left: 20%;width: 200px">
      <el-option
        v-for="item in liveOptions"
        :key="item.state"
        :label="item.state"
        :value="item.state"
      >
      </el-option>
    </el-select>

    <el-select v-model="abrogateIdentity" clearable placeholder="作废状态" style="margin-left: 40px;width: 200px">
      <el-option
        v-for="item in abrogateIdentitys"
        :key="item.abrogateIdentity"
        :label="item.abrogateIdentity"
        :value="item.abrogateIdentity"
      >
      </el-option>
    </el-select>

    <el-input
      v-model="liveId"
      placeholder="订单ID"
      name="roomNumber"
      type="text"
      clearable
      style="margin-left:40px ;width: 180px"
    />


    <el-button plain
               style="width:80px;margin-bottom:30px;margin-left:40px ;background-color: #cfefc6; border-radius: 8px; box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);"
               @click="selectAll"
    >
      查询
    </el-button>
    <el-button
      style="width:80px;margin-bottom:30px;margin-left:40px ;background-color: white; border-radius: 8px; box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);"
      @click="checkLive"
    >重置
    </el-button>
    <el-table :data="liveList.slice((currentPage-1)*pagesize,currentPage*pagesize)" height="659px" border style="width: 100%;margin-top: 20px"
              class="elegant-room-table"
    >
      <el-table-column align="center" fixed prop="liveId" label="订单ID" min-width="100"/>
      <el-table-column align="center" fixed prop="customerName" label="顾客名称" width="100"/>
      <el-table-column align="center" fixed prop="roomNumber" label="房间号" min-width="100"/>
      <el-table-column align="center" fixed prop="roomTypeName" label="房间类型" min-width="100"/>
      <el-table-column align="center" fixed prop="roomPrice" label="房间价格" width="100">
        <template v-slot="scope">
          <span class="price-tag">¥{{ scope.row.roomPrice }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" fixed prop="liveTime" label="入住时间" width="110"/>
      <el-table-column align="center" fixed prop="cancelTime" label="退房时间" width="110"/>
      <el-table-column align="center" fixed prop="state" label="房间状态" min-width="110">
        <template v-slot="scope">
    <span :class="{
        'state-green': scope.row.state === '已退房',
        'state-red': scope.row.state === '入住中'
      }"
    >
      {{ scope.row.state }}
    </span>
        </template>
      </el-table-column>
      <el-table-column align="center" fixed prop="liveAmount" label="订单金额" min-width="110"/>
      <el-table-column align="center" fixed prop="collectionIdentity" label="收款标识" min-width="110">
        <template v-slot="scope">
    <span :class="{
        'collectionIdentity-green': scope.row.collectionIdentity === '已收款',
        'collectionIdentity-red': scope.row.collectionIdentity === '未收款'
      }"
    >
      {{ scope.row.collectionIdentity }}
    </span>
        </template>
      </el-table-column>
      <el-table-column align="center" fixed prop="remark" label="房间备注" min-width="130"/>
      <el-table-column align="center" fixed prop="abrogateIdentity" label="作废标识" width="120"/>
      <el-table-column align="center" fixed label="操作" width="120">
        <template v-slot="scope">
          <!--  退房、作废、入住 -->
          <el-button v-if="scope.row.state == '已退房' && scope.row.collectionIdentity == '未收款' && !scope.row.liveAmount" type="text"
                     @click="settlementLiveButton(scope.row)">结算</el-button>
          <el-button v-if="scope.row.state == '已退房' && scope.row.collectionIdentity == '未收款' && scope.row.liveAmount" type="text"
                     @click="collectionLiveButton(scope.row)">确认收款</el-button>
        </template>
      </el-table-column>
    </el-table>

    <div style="margin-top:5px;margin-left: 30%;">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[5,8,10,15,20]"
        :page-size="pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="liveList.length">
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { deleteRoomType, insert1RoomType, selectAllRoomType, updateRoomType } from '@/api/roomType'
import { insertCustomer, selectAllCustomer, updateCustomer } from '@/api/customer'
import { selectAllLiveRecords } from '@/api/liveRecords'
import { abrogateStay1, cancelStay, insertStay, selectAllStay1 } from '@/api/stay'
import { collectionLive1, selectAllLive, settlementLive1 } from '@/api/live'

export default {
  name: 'user',
  computed: {
    ...mapGetters([
      'userId',
      'userName',
      'roleName'
    ])
  },
  data() {
    return {
      state: '',
      abrogateIdentity: '',
      liveId: '',
      liveList: [],
      liveOptions: [], // 下拉
      abrogateIdentitys: [],
      collectionController:false,
      collectionList:{
        collectionIdentity:'',
      },
      settlementLiveController:false,
      settlementLiveList:{
        liveAmount:'',
        roomPrice:'',
      },
      currentPage: 1,
      currentIndex: '',
      pagesize: 8,
    }
  },
  mounted() {
    this.selectAll()
    this.fetchLive()
    this.fetchAbrogateIdentity()
  },
  methods: {
    handleSizeChange(val) {
      this.pagesize = val;
      console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      this.currentPage= val
    },

    selectAll() {
      selectAllLive({
        state: this.state,
        abrogateIdentity:this.abrogateIdentity,
        liveId: this.liveId
      }).then(
        response => {
          console.log(response.data)
          this.liveList = response.data
          if((this.liveList.slice((this.currentPage-1)*this.pagesize,this.currentPage*this.pagesize)).length == 0){
            this.currentPage = 1
          }
        }
      )
    },
    collectionAllLive(formName) {
      collectionLive1({
        collectionIdentity: this.collectionList.collectionIdentity,
        liveTableId: this.collectionList.liveTableId
      }).then(response => {
        this.collectionController = false;
        this.$message.success('确认收款成功');
        this.abrogateIdentity = ''
        this.state = ''
        this.liveId = ''
        this.selectAll();
      }).catch(error => {
        console.error(error);
        this.$message.error('确认收款失败');
      });
    },
    settlementAllLive(formName) {
      settlementLive1({
        liveAmount: this.settlementLiveList.liveAmount,
        roomPrice: this.settlementLiveList.roomPrice,
        liveTableId: this.settlementLiveList.liveTableId
      }).then(response => {
        this.settlementLiveController = false;
        this.$message.success('结算成功');
        this.selectAll();
      }).catch(error => {
        console.error(error);
        this.$message.error('结算失败');
      });
    },
    // 下拉
    fetchLive() {
      selectAllLive().then(response => {
        this.liveOptions = Object.values(
          response.data.reduce((selectState, item) => {
            selectState[item.state] = item
            return selectState
          }, {})
        )
      })
    },
    fetchAbrogateIdentity() {
      selectAllLive().then(response => {
        this.abrogateIdentitys = Object.values(
          response.data.reduce((selectAbrogateIdentity, item) => {
            selectAbrogateIdentity[item.abrogateIdentity] = item
            return selectAbrogateIdentity
          }, {})
        )
      })
    },
    settlementLiveButton(row){
      this.settlementLiveList = {
        liveAmount: row.roomPrice,
        liveTableId: row.liveTableId
      }
      this.settlementLiveController = true
    },
    collectionLiveButton(row){
      this.collectionList = {
        collectionIdentity: '已收款',
        liveTableId: row.liveTableId
      }
      this.collectionController = true
    },
    checkLive() {
      this.state=''
      this.abrogateIdentity=''
      this.liveId=''
      this.selectAll()
    },
    closeCollection(){
      this.collectionController = false;
    },
    closeSettlement(){
      this.settlementLiveController = false;
    },
  }
}
</script>

<style lang="scss" scoped>
.dashboard {
  &-container {
    margin: 30px;
  }

  &-text {
    font-size: 30px;
    line-height: 46px;
  }
}

.elegant-room-table {
  font-family: 'Segoe UI', 'PingFang SC', 'Microsoft YaHei', sans-serif;
  box-shadow: 0 4px 12px 0 rgba(0, 0, 0, 0.05);
  border-radius: 8px;
  overflow: hidden;

  &::before {
    display: none;
  }

  .price-tag {
    display: inline-block;
    padding: 3px 8px;
    color: #171717;
    border-radius: 12px;
    font-weight: 500;
  }
}

.state-green {
  color: green;
}

.state-red {
  color: red;
}
</style>
